# -*- coding: utf-8 -*-

"""Unimail
@version: 0.14
@license: GPL 3.0 (see "license" file)
@author: Jose A. Jimenez
@contact: jajimc@gmail.com"""

import sys
import os
import gtk
import core

DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))

class DialAssistant:
    """Class that represents the About window."""
    def __init__(self):
        self.__gladefile = os.path.join(DIR, "dialassistant.glade")
        self.__builder = gtk.Builder()
        self.__builder.add_from_file(self.__gladefile)
        self.__dialog = self.__builder.get_object("dialAssistant")
        
        self.__vboxEmailAccount = self.__builder.get_object("vboxEmailAccount")
        self.__entEmailAddress = self.__builder.get_object("entEmailAddress")
        self.__entEmailPassword = self.__builder.get_object("entEmailPassword")
        self.__chkSaveEmailPassword = self.__builder.get_object("chkSaveEmailPassword")
        
        self.__vboxEmailName = self.__builder.get_object("vboxEmailName")
        self.__entEmailName = self.__builder.get_object("entEmailName")
        
        self.__defaultEntryTextColor = self.__entEmailAddress.get_style().text[0]
        
        self.__builder.connect_signals(self)
        self.__init_data()
        
    def __init_data(self):
        self.__saveEmailPasswordMarked = True
        self.__emailPassword = ""
        self.__result = gtk.RESPONSE_NONE
        self.__settings = None
        
    def run(self):
        self.__dialog.show_all()
        gtk.main()
        self.__dialog.destroy()
        return self.__result
        
    def get_settings(self):
        return self.__settings
        
    def chksaveemailpassword_toggled(self, widget):
        save = self.__chkSaveEmailPassword.get_active()
        self.__entEmailPassword.set_sensitive(save)
        
        if not save:
            self.__entEmailPassword.set_text("")
            
    def email_account_data_changed(self, widget):
        emailAddress = self.__entEmailAddress.get_text()
        condition1 = core.validation.validate_email_address(emailAddress)
        
        emailPassword = self.__entEmailPassword.get_text()
        saveEmailPasswordMarked = self.__chkSaveEmailPassword.get_active()     
        condition2 = ((saveEmailPasswordMarked and emailPassword != "") or (not saveEmailPasswordMarked))
        
        if condition1:
            self.__entEmailAddress.modify_text(gtk.STATE_NORMAL, self.__defaultEntryTextColor)
        else:
            self.__entEmailAddress.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse("#FF0000"))
        
        self.__dialog.set_page_complete(self.__vboxEmailAccount, condition1 and condition2)
        
    def entemailname_changed(self, widget):
        self.__dialog.set_page_complete(self.__vboxEmailName, self.__entEmailName.get_text() != "")
        
    def dialassistant_apply(self, widget):
        #ToDo
        self.__settings = configuration.get_default_settings()
        
        emailAddress = self.__emailAddress.get_text()
        emailPassword = self.__emailPassword.get_text()
        emailName = self.__emailName.get_text()
        
        self.__settings.set_email_address(emailAddress)
        self.__settings.set_email_name(emailName)
        
        userId = emailAddress[:emailAddress.find("@")]
        self.__settings.set_imap_user_id(userId)
        self.__settings.set_imap_user_password(emailPassword)
        
        self.__settings.set_smtp_user_id(userId)
        self.__settings.set_smtp_user_password(emailPassword)
        
        domain = emailAddress[emailAddress.find("@") + 1:]
        imapServer = "imap.%s" % domain
        smtpServer = "smtp.%s" % domain
        self.__settings.set_imap_server(imapServer)
        self.__settings.set_smtp_server(smtpServer)
        
        
            
        self.__result = gtk.RESPONSE_OK
        gtk.main_quit()
        
    def dialassistant_cancel(self, widget):
        self.__result = gtk.RESPONSE_CANCEL
        gtk.main_quit()
